package com.fly.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.fly.dto.User;
import com.fly.dto.UserQuery;
import com.fly.exception.UserNotExistException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping
//    public List<User> query(@RequestParam(name = "username", required = false, defaultValue = "匿名") String username) {
    @JsonView(User.UserSimpleView.class)
    public List<User> query(UserQuery userQuery, @PageableDefault(size = 9, page = 3) Pageable pageable) {
//        System.out.println(username);
//        System.out.println(userQuery.toString());
        System.out.println(ReflectionToStringBuilder.toString(userQuery, ToStringStyle.MULTI_LINE_STYLE));
        System.out.println(pageable.getPageNumber());
        System.out.println(pageable.getPageSize());
        System.out.println(pageable.getSort());
        List<User> list = new ArrayList<>();
        list.add(new User("张三","123"));
        list.add(new User());
        list.add(new User());
        return list;
    }

    @GetMapping("/{id:\\d+}") //只接受数字
    @JsonView(User.UserDetailView.class)
    public User getInfo(@PathVariable("id") String iddddd) {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123");
        return user;
    }

    /**
     * Valid绑定字段验证
     * @param user
     * @param errors 绑定返回
     * @return
     */
    @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult errors){
        if (errors.hasErrors()){
            errors.getAllErrors().stream().forEach(error->System.out.println(error.getDefaultMessage()));
        }
        System.out.println(user);
        System.out.println(user.getBirthday());
        user.setId("1");
        return user;
    }
    @PutMapping("/{id:\\d+}")
    public User update(@Valid @RequestBody User user, BindingResult errors){
        if (errors.hasErrors()){
            errors.getAllErrors().stream().forEach(error->System.out.println(error.getDefaultMessage()));
        }
        System.out.println(user);
        System.out.println(user.getBirthday());
        user.setId("1");
        return user;
    }

    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable String id){
        System.out.println(id);
    }
    @GetMapping("/test")
    public String test(){
        throw new RuntimeException("运行时异常");
    }

    @GetMapping("/test/{id:\\d+}")
    @ApiOperation(value = "用户查询测试")
    public String test2(@ApiParam("用户id") @PathVariable String id){
//        throw new UserNotExistException(id);
        return "123";
    }
}
